<?php
/**
 * $Id$
 *  @package	Joomla
 *  @author Daniel Eliasson
* 	@copyright	Copyright (C) 2010 Stilero. All rights reserved.
* 	@license	GNU/GPL, see LICENSE.php
* 	Joomla! is free software. This version may have been modified pursuant
* 	to the GNU General Public License, and as distributed it includes or
* 	is derivative of works licensed under the GNU General Public License or
* 	other free or open source software licenses.
*/

// no direct access
defined('_JEXEC') or die('Restricted access'); 
?>
<div class="moduletable<?php echo $params->get('moduleclass_sfx'); ?>">
		<?php $i=1; ?>
		<?php 
		$parentid="0";
		//Iterate the list to find the parent
		foreach ($list['category_tmp'] as $item){
			if(JRequest::getVar('category_id')==$item['category_child_id']){
				//If its a child, we set the parent id.
				$parentid=($item['category_parent_id']==0)?$item['category_child_id']:$item['category_parent_id'];
			}
		}
		
		//Iterate the same list to print the menu
		foreach ($list['category_tmp'] as $item){?>
			<?php 	
				//Allways print maincategories if parent_id=0 or if parentid bigger than 0
				if($item['category_parent_id']==0 || $parentid>0) { ?>
					<ul class="level1" >
					<?php $parentlink = JRoute::_('index.php?option=com_virtuemart&amp;page=shop.browse&amp;Itemid=2&amp;category_id='.$item['category_child_id']); ?>
					<?php 
					$active="";
					$classid="";
					if(JRequest::getVar('category_id')==$item['category_child_id']){
						$active=" active";
						$classid =" id='current'";
					}
					//$active=(JRequest::getVar('category_id')==$item['category_child_id'])?' active':'';
					?>
					<?php  
					//Check if maincategory and then print out link
					if($item['category_parent_id']==0 ){?>
						<li<?php print $classid; ?> class="level1 item<?php print $i++; ?> parent<?php print $active; ?>" >
							<a href="<?php print $parentlink; ?>" class="parentcat parent<?php print $item['category_child_id'].$active; ?>"><?php print $item['category_name'] ?></a>
						</li>
					
						<?php	
						//Check if the parent is clicked and then show all its children.
						//Also check if the clicked menuitem is a child
						if($item['category_child_id']==$parentid) {					
						
							//Check if chache is activated
							if ($params->get('cache')){
								//Force cache activate	
								$cache =& JFactory::getCache('mod_vm_categoryshortcut');
								$cache->setCaching( 1 );
								$method = array($ps_product_category, 'get_child_list');					
								$children = $cache->call($method, $item['category_child_id']);
							}else{
								$cache =& JFactory::getCache('mod_vm_categoryshortcut');
								$children = $ps_product_category->get_child_list($item['category_child_id']);
							}
							
							if ($children!=""){ 
								?>
							
								<ul class="level2">
									<?php $j=1; ?>
									<?php foreach($children as $child) { ?>						
										<?php $childlink = JRoute::_('index.php?option=com_virtuemart&amp;page=shop.browse&amp;Itemid=2&amp;category_id='.$child['category_id']); ?>
										<?php
										$activechild="";
										$classidchild="";
										if(JRequest::getVar('category_id')==$child['category_child_id']){
											$activechild=" active";
											$classidchild =" id='current'";
										} 
										//$activechild="";
										//$activechild=(JRequest::getVar('category_id')==$child['category_id'])?' active':'';
										?>
										<li<?php print $classidchild?> class="level2 item<?php print $child['category_id'].$activechild?>"><a href="<?php print $childlink; ?>" class="childcat child<?php print $child['category_id'].$activechild?>"><?php print $child['category_name']; ?></a></li>
									<?php } ?>
								</ul>
							<?php } 
							}
						}
						?>
					
				
					</ul>
			<?php } ?>
		<?php } ?>
		<a href="<?php print JRoute::_('index.php?option=com_virtuemart&amp;page=shop.browse&amp;Itemid=2'); ?>" class="show_all" rel="nofollow"><?php echo JText::_("Show all"); ?></a>
		</div>